<?php
#error_log(' '..' ', 3, MY_LOG_FILE);
class PWP_ServiceAction {

	private $mdt;
	private $view;

	public function __construct() {
		add_action('wp_ajax_nopriv_getComments', array($this, 'getComments'));
	}
	
	public static function init() {
	}
	
	public static function destroy() {
	}

	public function getComments() {
		if ($this->hasPermission()) {
			$response = array();
			foreach($this->getMdt()->getComments() as $item) {
				$response['post'] = $item->post_title;
				$response['author'] = $item->comment_author;
				$response['date'] = $item->comment_date;
				$response['content'] = $item->comment_content;
			}
			$this->getView()->sendResponseJson($response);
		}
	}
	
	private function hasPermission() {
		$value = esc_attr($_POST['val']);
		$code = esc_attr($_POST['code']);
		return $code == $this->codeValue($value);
	}
	
	private function codeValue($value) {
		return hash('ripemd320', md5($this->getMdt()->getSiteKey() * $value));
	}
	
	private function getMdt() {
		if ($mdt == null) {
			$mdt = new PWP_Mdt();
		}
		return $mdt;
	}
	
	private function getView() {
		if ($view == null) {
			$view = new PWP_View();
		}
		return $view;
	}
}
?>
